package com.atguigu.eduService.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.event.AbstractIgnoreExceptionReadListener;
import com.atguigu.eduService.entity.EduChapter;
import com.atguigu.eduService.entity.EduVideo;
import com.atguigu.eduService.entity.chapter.Chapter;
import com.atguigu.eduService.mapper.EduChapterMapper;
import com.atguigu.eduService.mapper.EduVideoMapper;
import com.atguigu.eduService.service.EduChapterService;
import com.atguigu.serviceBase.exception.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.netty.buffer.UnpooledUnsafeHeapByteBuf;
import io.swagger.annotations.Authorization;
import javafx.scene.CacheHint;
import javafx.scene.media.VideoTrack;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.jgss.HttpCaller;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author smday
 * @since 2020-07-27
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Resource
    private EduChapterMapper chapterMapper;

    @Resource
    private EduVideoMapper videoMapper;

    @Override
    public List<Chapter> getAllChaptersByCourseId(String courseId) {

        //根据courseId找到所有的章节和小节
        //return chapterMapper.getAllChaptersByCourseId(courseId);

        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        //找到所有courseId的chapters
        List<EduChapter> eduChapters = baseMapper.selectList(wrapperChapter);

        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        //找到所有courseId的videos
        List<EduVideo> eduVideos = videoMapper.selectList(wrapperVideo);

        List<Chapter> chapterList = new ArrayList<>();

        for (EduChapter eduChapter : eduChapters) {
            Chapter chapter = new Chapter();
            BeanUtils.copyProperties(eduChapter,chapter);

            chapterList.add(chapter);

            List<Chapter> videoList = new ArrayList<>();
            for (EduVideo eduVideo : eduVideos) {
                if(eduVideo.getChapterId().equals(eduChapter.getId())){
                    Chapter video = new Chapter();
                    BeanUtils.copyProperties(eduVideo,video);
                    videoList.add(video);
                }
            }
            chapter.setChildren(videoList);
        }
        return chapterList;
    }


    @Override
    public int deleteChapterById(String chapterId) {
        //根据章节id查询小节表
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        Integer count = videoMapper.selectCount(wrapper);

        if(count>0){
            throw new GuliException("还有小节,不能删除");
        }
        return baseMapper.deleteById(chapterId);


    }

    @Override
    public List<Chapter> getChapter(String courseId) {
        return chapterMapper.getChapterByCourseId(courseId);
    }

    @Override
    public void deleteChapterByCourseId(String courseId) {

        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }


}
